package com.xiaoy.util.store.io;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import com.xiaoy.util.format.FileUtil;

public class ReadFileUtil {

	
	
	
	public static Map<String,String> readProperties(List<String>paths) {
		Map<String,String> result = new HashMap<String,String>();
		for (String path : paths){
			result.putAll(readProperties(path));
		}
		return result;
	}
	
	public static Map<String,String>readProperties (String path) {
		InputStream is = FileUtil.getInputStram(path.concat(".properties"));
		Properties props = new Properties();
		Map<String,String> result = new HashMap<String,String>();
		try {
			props.load(is);
			Iterator<Object> it = props.keySet().iterator();
			while (it.hasNext()){
				String key = (String)it.next();
				result.put(key,props.getProperty(key));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public static Map<String,String>readDirProperties (String path) {
		
		Map<String,String> result = new HashMap<String,String>();
		try {
			List<InputStream> iss = FileUtil.readAllFileByFolderPath(FileUtil.getPath(path));
			for (InputStream is : iss) {
				result.putAll(inputStreamToMap(is));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	
	public static Map<String,String> inputStreamToMap(InputStream is) throws IOException {
		
		Map<String,String> result = new HashMap<String,String>();
		Properties props = new Properties();
		props.load(is);
		Iterator<Object> it = props.keySet().iterator();
		while (it.hasNext()){
			String key = (String)it.next();
			result.put(key,props.getProperty(key));
		}
		return result;
	}
	
}
